﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MAT.Model;

namespace MAT.Controllers
{
    class ctrRequestBorrowAsset
    {
        private frmRequestBorrowAsset _frmRequestBorrowAsset = null;

        public frmRequestBorrowAsset FrmRequestBorrowAsset
        {
            get { return _frmRequestBorrowAsset; }
            set { _frmRequestBorrowAsset = value; }
        }

        MAT_Context context = MATContext.getContext();

        private bool check()
        {
            if (_frmRequestBorrowAsset.txtReason.Text.TrimStart().Length < 1 || _frmRequestBorrowAsset.txtReason.Text.TrimStart().Length > 200)
            {
                MessageBox.Show("Your reason is from 1 to 200 digits");
                _frmRequestBorrowAsset.txtReason.Focus();
                return false;
            }
            else
                if (_frmRequestBorrowAsset.dateTimeReturnDate.Value < _frmRequestBorrowAsset.dateTimeBorrowDate.Value)
                {
                    MessageBox.Show("You must fill the Returned Date >= Borrowed Date");
                    return false;
                }
                else
                    if (_frmRequestBorrowAsset.dateTimeBorrowDate.Value < DateTime.Today)
                    {
                        MessageBox.Show("Wrong Borrowed Date");
                        return false;
                    }
                    else
                        if (_frmRequestBorrowAsset.dateTimeReturnDate.Value < DateTime.Today)
                        {
                            MessageBox.Show("Wrong Returned Date");
                            return false;
                        }
                        else
                            return true;
        }

        public void frmRequestBorrowAsset_Load()
        {
            try
            {
                _frmRequestBorrowAsset.txtBorrower.Text = MAT.Controllers.ctrLogin.Username;
                List<CATEGORY> categories = new List<CATEGORY>();
                //List category of assets are Free status
                foreach (ASSET asset in context.ASSETs.Where(ass => ass.EMPLOYEE_ID == null).ToList())
                    categories.Add(asset.CATEGORY);
                _frmRequestBorrowAsset.cmbRALAssetCategory.DataSource = categories.ToList();
                _frmRequestBorrowAsset.cmbRALAssetCategory.DisplayMember = "CATEGORY_NAME";
                _frmRequestBorrowAsset.cmbRALAssetCategory.ValueMember = "CATEGORY_ID";
            }
            catch { }
        }

        public void btnRALClose_Click()
        {
            _frmRequestBorrowAsset.Close();
            MAT.Controllers.ctrMain._frmMain.pnlMain.Visible = false;
        }
        
        public void btnRALReset_Click()
        {
            _frmRequestBorrowAsset.cmbRALAssetCategory.Text = "";
            _frmRequestBorrowAsset.txtReason.Text = "";
            _frmRequestBorrowAsset.dateTimeBorrowDate.Text = "";
            _frmRequestBorrowAsset.dateTimeReturnDate.Text = "";
        }

        public void btnRALSend_Click()
        {
            if (_frmRequestBorrowAsset.cmbAssetName.Text != "")
            {
                if (check())
                {
                    REQUEST_BORROW request_borrow = new REQUEST_BORROW();
                    request_borrow.ASSET_ID = _frmRequestBorrowAsset.cmbAssetName.SelectedValue.ToString();
                    request_borrow.EMPLOYEE_ID = MAT.Controllers.ctrLogin.employee_ID;
                    request_borrow.REQUEST_DATE = _frmRequestBorrowAsset.dateTimeBorrowDate.Value;
                    request_borrow.RETURN_DATE = _frmRequestBorrowAsset.dateTimeReturnDate.Value;
                    request_borrow.REASON = _frmRequestBorrowAsset.txtReason.Text;
                    if (MAT.Controllers.ctrLogin.priority == 3 || MAT.Controllers.ctrLogin.priority == 2)
                        request_borrow.REQ_STATUS = "RE001";
                    else
                        request_borrow.REQ_STATUS = "RE003";
                    try
                    {
                        context.REQUEST_BORROW.AddObject(request_borrow);
                        context.SaveChanges();
                        MessageBox.Show("SUCCESSFUL!");
                        btnRALReset_Click();
                    }
                    catch { }
                }
            }
        }

        //This is action to reload the DataGrid when the selected value of comboBox Category is changed
        public void cmbRALAssetCategory_SelectedValueChanged()
        {
            try
            {
                string a = _frmRequestBorrowAsset.cmbRALAssetCategory.SelectedValue.ToString();
                List<ASSET> assets = context.ASSETs.Where(ass => ass.EMPLOYEE_ID == null).ToList();
                assets = assets.Where(ass => ass.CATEGORY_ID == a).ToList();
                _frmRequestBorrowAsset.cmbAssetName.DataSource = assets.ToList();
                _frmRequestBorrowAsset.cmbAssetName.DisplayMember = "ASSET_NAME";
                _frmRequestBorrowAsset.cmbAssetName.ValueMember = "ASSET_ID";
            }
            catch { }
        }
    }
}
